var Player = new Object;
Player.x = 0;
Player.y = 0;
Player.avatar = new Array("", "/resources/players/green.gif", "/resources/players/green.gif", "/resources/players/green.gif", "/resources/players/green.gif");
Player.facing = NORTH;
Player.map = 0;
Player.frommap = 0;


var OtherPlayers = new Object();
OtherPlayers.x = 0;
OtherPlayers.y = 0;
OtherPlayers.avatar = new Array("", "/resources/players/blue.gif", "/resources/players/blue.gif", "/resources/players/blue.gif", "/resources/players/blue.gif");
OtherPlayers.facing = NORTH;
OtherPlayers.map = 0;
OtherPlayers.frommap = 0;

var PlayerName = false;
var PlayerMap = false;

var HashPlayers = new Hashtable();

// players['playername'] = 


function playerName() {

	dwr.engine.beginBatch();

	PlayerMove.playerData( function(data) {
		//alert("playerName = = " + dwr.util.toDescriptiveString(data, 3));
		  PlayerName = data.playername;
		  document.getElementById('__fieldplayername').value=PlayerName;

		  PlayerMap = data.map;
		  document.getElementById('__fieldplayermap').value=PlayerMap;
	});


	dwr.engine.endBatch({
	  async:false
	});


	// alert("PlayerName = " + PlayerName);
}


function _loadPlayer(aPlayerInfo) {

	if (!HashPlayers.containsKey(aPlayerInfo.playername))
	{
		HashPlayers.put(aPlayerInfo.playername, _builPlayer(aPlayerInfo));
		
	} else{
			var _localAvatar = HashPlayers.get(aPlayerInfo.playername);
			//_localAvatar.style.top=IPixels[aPlayerInfo.x];
			//_localAvatar.style.left=IPixels[aPlayerInfo.y];
			_localAvatar.map = aPlayerInfo.map;
			_localAvatar.frommap = aPlayerInfo.frommap;

			log("pos = " + _localAvatar.map +" -- " + _localAvatar.frommap);
			if (_localAvatar.map != _localAvatar.frommap)
			{
				
				

				if (PlayerName==aPlayerInfo.playername)
				{
					PlayerMap = _localAvatar.map;
					//
					LoadMap(WorldMap[PlayerMap]);
				    FillMap();

					document.getElementById('__fieldplayermap').value=PlayerMap;
				}

			}
			_localAvatar.style.top=IPixels[aPlayerInfo.x];
			_localAvatar.style.left=IPixels[aPlayerInfo.y];
	}
	
		
	
}

function  _clearAvatarsFromMap (aPlayerInfo) {

var _lavatar = document.getElementById(aPlayerInfo.playername);
		if (aPlayerInfo.map!= PlayerMap)
		{
			_lavatar.style.visibility ="hidden";


		} else {
			_lavatar.style.visibility ="visible";
			_lavatar.className = "iplayer";
		}
}


function _builPlayer(aPlayerInfo) {

 var avatar = document.createElement('IMG');
 avatar.id = aPlayerInfo.playername;
 avatar.className = "iplayer";
 avatar.map = aPlayerInfo.map;
 avatar.frommap = aPlayerInfo.frommap;

if (PlayerName==aPlayerInfo.playername)
{
	 avatar.src = Player.avatar[Player.facing];
	 avatar.style.top=IPixels[aPlayerInfo.x];
	 avatar.style.left=IPixels[aPlayerInfo.y];
} else {
	 avatar.src = OtherPlayers.avatar[OtherPlayers.facing];
	 avatar.style.top=IPixels[aPlayerInfo.x];
	 avatar.style.left=IPixels[aPlayerInfo.y];
}

  avatar.zIndex = '3';
document.getElementById('pov').appendChild(avatar);
	


  return avatar;

}


function updatePlayers() {
	
	setInterval("_updatePlayers()",500);
	//setTimeout("_updatePlayers()",2000);
}


function _updatePlayers() {
	dwr.engine.beginBatch();

		PlayerMove.updatePlayers( function(data) {
					_updatePlayersPosition(data);

		});


	dwr.engine.endBatch({
	  async:true
	});
}

function _updatePlayersPosition(collection) {

	//alert(dwr.util.toDescriptiveString(collection, 3));
	var  len = collection.length;
	//alert("len = =  " + len);
	for (i=0; i< len ; i++)
	{
		_loadPlayer(collection[i]);
		_clearAvatarsFromMap(collection[i]);


	}

}


function SmoveEast() {
	
	PlayerMove.moveEast();
}

function SmoveSud() {

	PlayerMove.moveSud();
}

function SmoveWest() {
	PlayerMove.moveWest();
}

function SmoveNorth() {
	PlayerMove.moveNorth();
}

function SmoveNorthEast() {
	PlayerMove.moveNorthEast();
}

function SmoveSudEast() {
	PlayerMove.moveSudEast();
}

function SmoveSudWest() {
	PlayerMove.moveSudWest();
}

function SmoveNordWest() {
	PlayerMove.moveNordWest();
}
